package com.example.asus.a222dzd_ordering.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.asus.a222dzd_ordering.R;
import com.example.asus.a222dzd_ordering.base.Url;
import com.example.asus.a222dzd_ordering.bean.CollectListBean;

import com.squareup.picasso.Picasso;

import java.util.List;


public class CollectionFoodAdapter extends RecyclerView.Adapter<CollectionFoodAdapter.MyViewHolder> {

    private List<CollectListBean> beans;
    private FoodListAdapter.OnItemClickListener onItemClickListener;

    public interface OnItemClickListener{
        void setItem(View view, int position);
    }

    public void setOnItemClickListener(FoodListAdapter.OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<CollectListBean> beans){
        this.beans = beans;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_foodlist,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        CollectListBean bean = beans.get(i);
        final int position = i;
        Picasso.get()
                .load(Url.BASEUSRL+bean.getPic())
                .into(myViewHolder.iv_foodlist);
        myViewHolder.tv_foodlist_name.setText(bean.getFoodname());
        myViewHolder.tv_foodlist_price.setText(bean.getPrice()+" 元");
        if(onItemClickListener!=null){
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.setItem(v,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return beans==null?0:beans.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public View itemView;
        private ImageView iv_foodlist;
        private TextView tv_foodlist_name,tv_foodlist_price;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView=itemView;
            iv_foodlist=itemView.findViewById(R.id.iv_foodlist);
            tv_foodlist_name=itemView.findViewById(R.id.tv_foodlist_name);
            tv_foodlist_price=itemView.findViewById(R.id.tv_foodlist_price);
        }
    }
}
